home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 008a / fgdemo10.zip / FGDEMO.C < prev    next >
Text File  |  1991-10-05  |  14KB  |  485 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  main.c -- Fastgraph and Fastgraph/Light demo program v 1.0          *
  4. *                                                                      *
  5. *  This program is provided without warranty of any kind.  It may      *
  6. *  be distributed freely as long as the files are not modified.  You   *
  7. *  may use this source code in your own applications as long as it     *
  8. *  clearly understood that we are not responsible for any bugs in      *
  9. *  this code or in your code.  Use this code freely, use it in good    *
  10. *  health, but use it at your own risk.  Further disclaimers are       *
  11. *  listed at the end of this file.                                     *   
  12. *                                                                      *
  13. *  This program requires Fastgraph or Fastgraph/Light to link          *
  14. *  I compiled this program using the MSC 5.1 medium memory model       *
  15. *                                                                      *
  16. \**********************************************************************/
  17.  
  18. #include "defs.h"
  19.  
  20. int  menu_top;
  21. int  menu_bottom;
  22.  
  23. char matrix1[] = {0xAA,0x55,0xAA,0x55};
  24. char matrix2[] = {0x24,0x92,0x24,0x92};
  25.  
  26. /**********************************************************************\
  27. *                                                                      *
  28. *                                 main                                 *
  29. *                                                                      *
  30. \**********************************************************************/
  31.  
  32. void main()
  33. {
  34.    unsigned char key, aux;
  35.    int mousex, mousey, count;
  36.    register int i;
  37.    int current;
  38.  
  39.    /* initialize the video environment */
  40.  
  41.    initialize();
  42.  
  43.    /* read the 8pt and 14pt fonts */
  44.  
  45.    get_font();
  46.  
  47.    /* draw the screen on the hidden page and copy it to the visual page */
  48.  
  49.    draw_screen();
  50.    fg_restore(0,xlimit,0,ylimit);
  51.  
  52.    /* begin the main program loop */
  53.  
  54.    current = 0;
  55.    while (TRUE)
  56.    {
  57.       fg_mousevis(ON);
  58.       fg_waitfor(2);
  59.  
  60.       /* intercept a keystroke */
  61.  
  62.       fg_intkey(&key,&aux);
  63.       if (key == ESC)
  64.       {
  65.          exit_program();
  66.          continue;
  67.       }
  68.       else if (aux == RIGHT_ARROW)
  69.       {
  70.          current++;
  71.          if (current == ITEMS)
  72.             current = 0;
  73.       }
  74.       else if (aux == LEFT_ARROW)
  75.       {
  76.          current--;
  77.          if (current < 0)
  78.             current = ITEMS - 1;
  79.       }
  80.  
  81.       /* display menu according to keystroke pressed */
  82.  
  83.       if (key+aux > 0)
  84.       {
  85.          fg_mousevis(OFF);
  86.          current = horizontal_menu(main_menu,6,current,0,15,15,0);
  87.          if (current == ESC)
  88.          {
  89.             exit_program();
  90.             current = 0;
  91.          }
  92.          fg_mousevis(OFF);
  93.          fg_restore(0,xlimit,menu_top,ylimit);
  94.          fg_mousevis(ON);
  95.       }
  96.  
  97.       /* intercept a mouse click */
  98.  
  99.       fg_mousebut(1,&count,&mousex,&mousey);
  100.       if (count > 0 && BETWEEN(mousey,menu_top,menu_bottom))
  101.       {
  102.          for (i = 0; i <= ITEMS; i++)
  103.          {
  104.             if (BETWEEN(mousex, mouse_limits[i], mouse_limits[i+1]))
  105.             {
  106.                if (horizontal_menu(main_menu,6,i,0,15,15,0) == ESC)
  107.                   exit_program();
  108.                fg_mousevis(OFF);
  109.                fg_restore(0,xlimit,menu_top,ylimit);
  110.                fg_mousevis(ON);
  111.                break;
  112.             }
  113.          }
  114.       }
  115.    }
  116. }
  117.  
  118. /**********************************************************************\
  119. *                                                                      *
  120. *  draw_screen -- draw the main menu screen on the hidden page         *
  121. *                                                                      *
  122. \**********************************************************************/
  123.  
  124. void draw_screen()
  125. {
  126.    int y1, y2, y3;
  127.  
  128.    /* calculate y coordinates based on screen resolution */
  129.  
  130.    if (mode11 || mode16)
  131.    {
  132.       y1 = 3; y2 = 21; y3 = 38;
  133.    }
  134.    else
  135.    {
  136.       y1 = 1; y2 = 12; y3 = 23;
  137.    }
  138.  
  139.    menu_top = y2 + 1;
  140.    menu_bottom  = y3 + 1;
  141.  
  142.    /* erase the hidden page */
  143.  
  144.    background = 15;
  145.    fg_setpage(hidden);
  146.    fg_erase();
  147.  
  148.    /* draw some rectangles */
  149.  
  150.    fg_setcolor(15);
  151.    fg_rect(0,xlimit,0,ylimit);
  152.  
  153.    fg_setcolor(0);
  154.    fg_rect(0,xlimit,0,y2);
  155.  
  156.    /* draw the large dithered rectangles */
  157.  
  158.    if (mode06 || mode11)
  159.       fg_drect(0,xlimit,y3+1,ylimit,matrix1);
  160.    else
  161.    {
  162.       fg_setcolor(9);
  163.       fg_drect(0,xlimit,0,y2-1,matrix1);
  164.       fg_setcolor(14);
  165.       fg_drect(0,xlimit,y3+1,ylimit,matrix2);
  166.    }
  167.  
  168.    /* put the title in the title bar */
  169.  
  170.    fg_setcolor(15);
  171.    center_pstring("Fastgraph Demo",0,xlimit,y2-1);
  172.  
  173.    fg_setcolor(8);
  174.    draw_box(0,xlimit,0,ylimit);
  175.  
  176.    /* draw some black and grey outlines */
  177.  
  178.    fg_setcolor(7);
  179.    draw_box(1,xlimit-1,scale(1),ylimit-scale(1));
  180.    draw_box(2,xlimit-2,scale(2),ylimit-scale(2));
  181.  
  182.    fg_setcolor(0);
  183.    draw_box(3,xlimit-3,y1,y3);
  184.    draw_box(3,xlimit-3,y1,ylimit-y1);
  185.    draw_box(4,xlimit-4,y1,ylimit-y1);
  186.  
  187.    /* draw those little boxes in the corners */
  188.  
  189.    if (mode06 || mode11)
  190.    {
  191.       fg_drect(5,25,y1+1,y2-1,matrix1);
  192.       fg_drect(xlimit-25,xlimit-5,y1+1,y2-1,matrix1);
  193.    }
  194.    else
  195.    {
  196.       fg_setcolor(7);
  197.       fg_rect(5,25,y1+1,y2-1);
  198.       fg_rect(xlimit-25,xlimit-5,y1+1,y2-1);
  199.    }
  200.  
  201.    /* display the horizontal menu */
  202.  
  203.    horizontal_menu(main_menu,-6,0,0,15,15,0);
  204.    fg_setpage(visual);
  205. }
  206.  
  207. /**********************************************************************\
  208. *                                                                      *
  209. *  draw_window -- display a pop-up window                              *
  210. *                                                                      *
  211. \**********************************************************************/
  212.  
  213. void draw_window(xmin,xmax,ymin,ymax,string)
  214. int xmin, xmax, ymin, ymax;
  215. char *string;
  216. {
  217.    register int y;
  218.  
  219.    /* display the window and its border */
  220.  
  221.    fg_setcolor(15);
  222.    fg_rect(xmin,xmax,ymin,ymax);
  223.    fg_setcolor(0);
  224.    draw_box(xmin,xmax,ymin,ymax);
  225.    draw_box(xmin+1,xmax-1,ymin,ymax);
  226.  
  227.    /* display the header bar at the top of the window */
  228.  
  229.    y = ymin + ptsize + 1;
  230.    fg_rect(xmin,xmax,ymin,y+1);
  231.  
  232.    /* dither the header bar for 16 color modes */
  233.  
  234.    if (mode14 || mode16)
  235.    {
  236.       fg_setcolor(7);
  237.       fg_rect(xmin+2,xmin+22,ymin+1,y);
  238.       fg_rect(xmax-22,xmax-2,ymin+1,y);
  239.       fg_setcolor(9);
  240.       fg_drect(xmin+23,xmax-23,ymin+1,y,matrix1);
  241.    }
  242.  
  243.    /* dither the header bar for 2 color modes */
  244.  
  245.    else
  246.    {
  247.       fg_drect(xmin+2,xmin+22,ymin+1,y,matrix1);
  248.       fg_drect(xmax-22,xmax-2,ymin+1,y,matrix1);
  249.    }
  250.  
  251.    /* display the text in the window's header bar */
  252.  
  253.    fg_setcolor(15);
  254.    center_pstring(string,xmin,xmax,y+1);
  255.  
  256.    /* display the window's shadow */
  257.  
  258.    if (mode14 || mode16)
  259.       fg_setcolor(8);
  260.    else
  261.       fg_setcolor(0);
  262.  
  263.    fg_rect(xmin+3,xmax+3,ymax+1,ymax+scale(2));
  264.    fg_rect(xmax+1,xmax+3,ymin+scale(2),ymax);
  265. }
  266.  
  267. /**********************************************************************\
  268. *                                                                      *
  269. *  erase_window -- erase the previously displayed pop-up window        *
  270. *                                                                      *
  271. \**********************************************************************/
  272.  
  273. void erase_window(xmin,xmax,ymin,ymax)
  274. int xmin, xmax, ymin, ymax;
  275. {
  276.    register int x1, x2;
  277.  
  278.    /* restore the original area underneath the window */
  279.  
  280.    x1 = xmin & 0xFFF8;
  281.    x2 = ((xmax+3) & 0xFFF8) + 7;
  282.    fg_mousevis(OFF);
  283.    fg_restore(x1,x2,ymin,ymax+2);
  284.  
  285.    /* redraw the dither pattern on the hidden page */
  286.  
  287.    fg_setpage(hidden);
  288.    if (mode06 || mode11)
  289.       fg_drect(x1,x2,ymin,ymax+2,matrix1);
  290.    else
  291.    {
  292.       fg_setcolor(15);
  293.       fg_rect(x1,x2,ymin,ymax+2);
  294.       fg_setcolor(14);
  295.       fg_drect(x1,x2,ymin,ymax+2,matrix2);
  296.    }
  297.    fg_setpage(visual);
  298. }
  299.  
  300. /**********************************************************************\
  301. *                                                                      *
  302. *  get_answer -- accept a YES or NO answer from the keyboard or mouse  *
  303. *                                                                      *
  304. \**********************************************************************/
  305.  
  306. get_answer(xmin,xmax,y)
  307. {
  308.    int color;
  309.    int count, lastx, lasty;
  310.    int return_value;
  311.    int x;
  312.    int x1, x2, y1, y2;
  313.    char yes_no[4];
  314.    unsigned char key, aux;
  315.  
  316.    /* if using a mouse, display the "Yes" and "No" buttons */
  317.  
  318.    if (mouse)
  319.    {
  320.       color = fg_getcolor();
  321.       x1 = ((3 * xmin) + xmax) / 4;
  322.       x2 = ((3 * xmax) + xmin) / 4;
  323.       y1 = y - ptsize/2 - 1;
  324.       y2 = y + ptsize/2 + 1;
  325.       fg_setcolor(0);
  326.       draw_box(x1-25,x1+25,y1,y2);
  327.       draw_box(x1-24,x1+24,y1,y2);
  328.       draw_box(x2-25,x2+25,y1,y2);
  329.       draw_box(x2-24,x2+24,y1,y2);
  330.       center_pstring("Yes",x1,x1,y2);
  331.       center_pstring("No",x2,x2,y2);
  332.       fg_setcolor(8);
  333.       fg_rect(x1-23,x1+27,y2+1,y2+scale(2));
  334.       fg_rect(x1+26,x1+27,y1+scale(2),y2);
  335.       fg_rect(x2-23,x2+27,y2+1,y2+scale(2));
  336.       fg_rect(x2+26,x2+27,y1+scale(2),y2);
  337.       fg_mousevis(ON);
  338.    }
  339.  
  340.    /* read keystrokes until Y, y, N, n, or Esc is pressed */
  341.  
  342.    flushkey();
  343.    while (TRUE)
  344.    {
  345.       fg_waitfor(2);
  346.       fg_intkey(&key,&aux);
  347.       key = tolower(key);
  348.  
  349.       if (mouse)
  350.       {
  351.          fg_mousebut(1,&count,&lastx,&lasty);
  352.          if (BETWEEN(lastx,x1-25,x1+25) && BETWEEN(lasty,y1,y2))
  353.             key = 'y';
  354.          else if (BETWEEN(lastx,x2-25,x2+25) && BETWEEN(lasty,y1,y2))
  355.             key = 'n';
  356.       }
  357.  
  358.       if (key == 'y')
  359.       {
  360.          x = x1;
  361.          strcpy(yes_no,"Yes");
  362.          return_value = TRUE;
  363.          break;
  364.       }
  365.       else if (key == 'n' || key == ESC)
  366.       {
  367.          x = x2;
  368.          strcpy(yes_no,"No");
  369.          return_value = FALSE;
  370.          break;
  371.       }
  372.    }
  373.  
  374.    /* if using the mouse, flash the selected button */
  375.  
  376.    if (mouse)
  377.    {
  378.       fg_mousevis(OFF);
  379.       fg_setcolor(0);
  380.       fg_rect(x-23,x+23,y1+1,y2-1);
  381.       fg_setcolor(15);
  382.       center_pstring(yes_no,x,x,y2);
  383.       fg_waitfor(2);
  384.       fg_setcolor(15);
  385.       fg_rect(x-23,x+23,y1+1,y2-1);
  386.       fg_setcolor(0);
  387.       center_pstring(yes_no,x,x,y2);
  388.       fg_waitfor(2);
  389.       fg_setcolor(color);
  390.    }
  391.  
  392.    return(return_value);
  393. }
  394.  
  395. /**********************************************************************\
  396. *                                                                      *
  397. *  info_window -- display text information in a window                 *
  398. *                                                                      *
  399. \**********************************************************************/
  400.  
  401. void info_window(xmin,xmax,ymin,string,nstrings)
  402. int xmin,xmax,ymin;
  403. char *string[];
  404. int nstrings;
  405. {
  406.    register int i;
  407.    int x, y;
  408.    int miny, maxy;
  409.    int len, maxlen;
  410.    int ptsize1;
  411.  
  412.    /* center text in window according to length of longest string */
  413.  
  414.    ptsize1 = ptsize + 1;
  415.    maxlen = 0;
  416.    for (i = 1; i < nstrings; i++)
  417.    {
  418.       len = length_pstring(string[i]);
  419.       if (len > maxlen) maxlen = len;
  420.    }
  421.  
  422.    /* calculate top and bottom of info window */
  423.  
  424.    miny = scale(ymin);
  425.    maxy = miny + (nstrings+1) * (ptsize1);
  426.  
  427.    /* draw the info window on the hidden page first */
  428.  
  429.    fg_setpage(hidden);
  430.  
  431.    /* draw the window and put the first string in the header bar */
  432.  
  433.    draw_window(xmin,xmax,miny,maxy,string[0]);
  434.  
  435.    /* calculate starting point for text */
  436.  
  437.    x = xmin + ((xmax - xmin) - maxlen) / 2;
  438.    y = miny + ((ptsize+1)*2) + ptsize/2;
  439.  
  440.    /* put all the strings in the rest of the window */
  441.  
  442.    fg_setcolor(0);
  443.    for (i = 1; i < nstrings; i++)
  444.    {
  445.       put_pstring(string[i],x,y);
  446.       y += ptsize1;
  447.    }
  448.  
  449.    /* restore the window to the visual page */
  450.  
  451.    fg_setpage(visual);
  452.    fg_restore(xmin,xmax+3,miny,maxy+2);
  453.  
  454.    /* redraw the dither pattern on the hidden page */
  455.  
  456.    fg_setpage(hidden);
  457.    if (mode06 || mode11)
  458.       fg_drect(xmin,xmax+3,miny,maxy+2,matrix1);
  459.    else
  460.    {
  461.       fg_setcolor(15);
  462.       fg_rect(xmin,xmax+3,miny,maxy+2);
  463.       fg_setcolor(14);
  464.       fg_drect(xmin,xmax+3,miny,maxy+2,matrix2);
  465.    }
  466.    fg_setpage(visual);
  467.  
  468. }
  469.  
  470. /**********************************************************************\
  471. *                                                                      *
  472. *                         Further Disclaimers                          *
  473. *                                                                      *
  474. * Under no circumstances shall Ted Gruber Software be liable for any   *
  475. * loss of profit or any other commercial damage, including but not     *
  476. * limited to special, incidental, consequential, or other damages      *
  477. * resulting from the use of or the inability to use the software,      *
  478. * even if Ted Gruber Software has been notified of the possiblity      *
  479. * of such damages. Ted Gruber Software disclaims all other warranties, *
  480. * either expressed or implied, regarding the merchantability or        *
  481. * fitness of this software and accompanying documentation for any      *
  482. * particular application or purpose.                                   *
  483. *                                                                      *
  484. \**********************************************************************/
  485.